/*
 * Created by SharpDevelop.
 * User:Krug
 * Date: 2009-7-16
 * Time: 21:39
 * 
 * 
 */
using System;
using Toyger.UI;
using Toyger.Common.PlugIn;
using Toyger.Common.PlugIn.Base;
using System.Collections.Generic;
using System.Windows.Forms;
using Toyger.Common.Connections;
using Toyger.Common.UI;
using Toyger.Common.PlugIn.SqlServer2005;
using Toyger.Common.Interfaces;
namespace Toyger.SystemControl
{
    /// <summary>
    /// Description of Controller.
    /// </summary>
    public class Controller : IController
    {
        #region Singleton instance
        static readonly object aObject = new Object();
        static Controller instance;
        public static Controller Instance
        {
            get
            {
                lock (aObject)
                {
                    if (instance == null)
                    {
                        instance = new Controller();
                    }
                    return instance;
                }
            }
        }
        #endregion

        MainForm mainForm;
        List<ConnectionObject> connectionObjects = new List<ConnectionObject>();
        public MainForm MainForm
        {
            get { return mainForm; }
        }

        private Controller()
        {
            // init objects
            LoadConnectionObjects();


            //create MainForm
            this.mainForm = new MainForm(this);

#if DEBUG
            PlugInsManager.RegisteDBManager("Sql Server 2005", new SqlServer2005());
#endif
        }

        private void LoadConnectionObjects()
        {
#if DEBUG
            connectionObjects.Add(ConnectionObject.CreateConnectionObject("conn1", "connaString", "Sql Server 2005"));
            connectionObjects.Add(ConnectionObject.CreateConnectionObject("conn2", "connaString", "Sql Server 2005"));
            connectionObjects.Add(ConnectionObject.CreateConnectionObject("conn3", "connaString", "Sql Server 2005"));
#endif
        }

        internal void NewPage(ConnectionObject cObj)
        {
            Serverpage sp = new Serverpage(PlugInsManager.CreateSession(cObj));
            mainForm.AddPage(sp.TabPage);
        }


        internal object[] GetObjectList()
        {
            String[] names = connectionObjects.ConvertAll<String>(x => x.Name).ToArray();
            return names;

        }

        internal ConnObjPane GetSummuryPane(String objectName)
        {
            ConnectionObject cObj = connectionObjects.Find(x=>x.Name.Equals(objectName, StringComparison.OrdinalIgnoreCase));

            return PlugInsManager.GetServer(cObj.ServerType).GetConnObjPane(cObj, this);
        }

        /// <summary>
        /// Get the list of all avaliable server types.
        /// </summary>
        /// <returns></returns>
        internal string[] GetServerTypeList()
        {
            return PlugInsManager.GetServerList();
        }

        /// <summary>
        /// Create a new <c>ConnectionObject</c>
        /// </summary>
        /// <param name="p"></param>
        internal void NewConnectionObject(string servertype)
        {
            ConnectionObject co = PlugInsManager.GetServer(servertype).CreateDefaultConnectionObject();
            this.connectionObjects.Add(co);
            mainForm.AddNewConnectionObject(co.Name);
        }

        #region IController Members

        public void SaveConnectionObject(ConnectionObject cObj)
        {
            SystemDataLayer.SaveConnectionObject(cObj);
        }

        #endregion
    }
}
